import React, { useEffect } from 'react';
import { Form, Input, DatePicker, Card, Button, message, InputNumber } from 'antd';
import { useNavigate, useParams } from 'react-router-dom';
import { createSalary, updateSalary, getSalary } from '@/api/salary';
import type { SalaryDTO } from '@/api/salary';
import dayjs from 'dayjs';

const SalaryForm: React.FC = () => {
  const navigate = useNavigate();
  const { id } = useParams<{ id: string }>();
  const [form] = Form.useForm();
  const isEdit = !!id && id !== 'new';

  useEffect(() => {
    if (isEdit) {
      fetchData();
    }
  }, [id]);

  const fetchData = async () => {
    try {
      const data = await getSalary(Number(id));
      form.setFieldsValue({
        ...data,
        salaryMonth: data.salaryMonth ? dayjs(data.salaryMonth) : undefined,
      });
    } catch (error) {
      // 错误已在request.ts中处理
    }
  };

  const handleSubmit = async (values: any) => {
    try {
      const data: SalaryDTO = {
        ...values,
        salaryMonth: values.salaryMonth?.format('YYYY-MM'),
      };

      if (isEdit) {
        await updateSalary(Number(id), data);
        message.success('更新成功');
      } else {
        await createSalary(data);
        message.success('创建成功');
      }

      navigate('/salaries');
    } catch (error) {
      // 错误已在request.ts中处理
    }
  };

  return (
    <Card title={isEdit ? '编辑工资信息' : '新增工资信息'}>
      <Form
        form={form}
        layout="vertical"
        onFinish={handleSubmit}
      >
        <Form.Item
          name="className"
          label="所在班级"
          rules={[{ required: true, message: '请输入所在班级' }]}
        >
          <Input placeholder="请输入所在班级" />
        </Form.Item>

        <Form.Item
          name="position"
          label="职务"
          rules={[{ required: true, message: '请输入职务' }]}
        >
          <Input placeholder="请输入职务" />
        </Form.Item>

        <Form.Item
          name="name"
          label="姓名"
          rules={[{ required: true, message: '请输入姓名' }]}
        >
          <Input placeholder="请输入姓名" />
        </Form.Item>

        <Form.Item
          name="idCard"
          label="身份证号"
          rules={[
            { required: true, message: '请输入身份证号' },
            { pattern: /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[0-9Xx]$/, message: '请输入正确的身份证号' },
          ]}
        >
          <Input placeholder="请输入身份证号" />
        </Form.Item>

        <Form.Item
          name="identifier"
          label="标识号"
          rules={[{ required: true, message: '请输入标识号' }]}
        >
          <Input placeholder="请输入标识号" />
        </Form.Item>

        <Form.Item
          name="salaryMonth"
          label="工资月份"
          rules={[{ required: true, message: '请选择工资月份' }]}
        >
          <DatePicker picker="month" style={{ width: '100%' }} />
        </Form.Item>

        <Form.Item
          name="basicSalary"
          label="基础工资"
          rules={[{ required: true, message: '请输入基础工资' }]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={2}
            placeholder="请输入基础工资"
          />
        </Form.Item>

        <Form.Item
          name="performanceSalary"
          label="绩效工资"
          rules={[{ required: true, message: '请输入绩效工资' }]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={2}
            placeholder="请输入绩效工资"
          />
        </Form.Item>

        <Form.Item
          name="individualBonus"
          label="单项奖"
          rules={[{ required: true, message: '请输入单项奖' }]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={2}
            placeholder="请输入单项奖"
          />
        </Form.Item>

        <Form.Item
          name="enterprisePension"
          label="企业年金"
          rules={[{ required: true, message: '请输入企业年金' }]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={2}
            placeholder="请输入企业年金"
          />
        </Form.Item>

        <Form.Item
          name="endowmentInsurance"
          label="养老保险"
          rules={[{ required: true, message: '请输入养老保险' }]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={2}
            placeholder="请输入养老保险"
          />
        </Form.Item>

        <Form.Item
          name="unemploymentInsurance"
          label="失业保险"
          rules={[{ required: true, message: '请输入失业保险' }]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={2}
            placeholder="请输入失业保险"
          />
        </Form.Item>

        <Form.Item
          name="medicalInsurance"
          label="医疗保险"
          rules={[{ required: true, message: '请输入医疗保险' }]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={2}
            placeholder="请输入医疗保险"
          />
        </Form.Item>

        <Form.Item
          name="housingFund"
          label="住房公积金"
          rules={[{ required: true, message: '请输入住房公积金' }]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={2}
            placeholder="请输入住房公积金"
          />
        </Form.Item>

        <Form.Item
          name="personalTax"
          label="个税"
          rules={[{ required: true, message: '请输入个税' }]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={2}
            placeholder="请输入个税"
          />
        </Form.Item>

        <Form.Item
          name="deductionOne"
          label="补扣项一"
        >
          <InputNumber
            style={{ width: '100%' }}
            precision={2}
            placeholder="请输入补扣项一"
          />
        </Form.Item>

        <Form.Item
          name="deductionTwo"
          label="补扣项二"
        >
          <InputNumber
            style={{ width: '100%' }}
            precision={2}
            placeholder="请输入补扣项二"
          />
        </Form.Item>

        <Form.Item>
          <Button type="primary" htmlType="submit">
            {isEdit ? '保存' : '创建'}
          </Button>
          <Button style={{ marginLeft: 8 }} onClick={() => navigate('/salaries')}>
            取消
          </Button>
        </Form.Item>
      </Form>
    </Card>
  );
};

export default SalaryForm; 